package com.pm.controller.dept;

import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.common.Response;
import com.pm.common.annotation.CurrentUser;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.dto.dept.DeptTypeScoreDto;
import com.pm.entity.po.dept.DepartmentScore;
import com.pm.entity.po.dept.EvaluateTaskPo;
import com.pm.entity.po.log.DepartmentScoreCheckLog;
import com.pm.entity.po.security.User;
import com.pm.entity.vo.ScoreSummaryVo;
import com.pm.entity.vo.dept.DepartmentScoreVo;
import com.pm.entity.vo.dept.DeptQuarterScoreVo;
import com.pm.entity.vo.dept.DeptTaskNumVo;
import com.pm.entity.vo.dept.DeptTypeTaskScoreVo;
import com.pm.service.dept.DepartmentScoreService;
import com.pm.service.log.DepartmentScoreCheckLogService;
import com.pm.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api(tags = "配合单位打分")
@UserLoginToken
@RestController
@RequestMapping("/deptScore")
public class DepartmentScoreController {
    @Autowired
    private DepartmentScoreService departmentScoreService;
    @Autowired
    private DepartmentScoreCheckLogService departmentScoreCheckLogService;

    @ApiOperation("新增接口")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value = "新增单位打分信息")
    public Response<Integer> add(DepartmentScore departmentScore) {
        Response<Integer> response = null;
        Integer result = departmentScoreService.add(departmentScore);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("批量新增接口")
    @PostMapping("/addAll")
    @ResponseBody
    @MyLog(value = "新增单位打分信息")
    public Response addAll(@RequestBody List<DepartmentScore> departmentScore) {
        departmentScoreService.addAll(departmentScore);
        return new Response<>(ExpCodeState.add_data_success);
    }

    @ApiOperation("更新接口")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "编辑单位打分信息")
    public Response<Integer> update(DepartmentScore departmentScore) {
        Response<Integer> response = null;
        Integer result = departmentScoreService.update(departmentScore);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("提交审核")
    @PostMapping("/auditing")
    @ResponseBody
    @MyLog(value = " ")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> auditing(@RequestBody DepartmentScore departmentScore, @CurrentUser User user) {
        Response<Integer> response = null;
        Integer result = departmentScoreService.update1(departmentScore);
        DepartmentScoreCheckLog clog = new DepartmentScoreCheckLog();
        //clog.setYear(departmentScore.getYear());
        clog.setRead(0);
        clog.setStatus(0);
        clog.setQuarter(departmentScore.getQuarter());
        clog.setTaskId(departmentScore.getTaskId());
        clog.setApplicat(user.getUserId());
        clog.setAddTime(new Date());
        departmentScoreCheckLogService.add(clog);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("删除接口")
    @PostMapping("/remove")
    @ResponseBody
    @MyLog(value = "删除单位打分信息")
    public Response<Integer> remove(DepartmentScore departmentScore) {
        Response<Integer> response = null;
        Integer result = departmentScoreService.remove(departmentScore);
        if (result > 0) {
            response = new Response<>(ExpCodeState.remove_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.remove_data_failed);
        }
        return response;
    }

    @ApiOperation("单个对象查询接口")
    @PostMapping("/load")
    @ResponseBody
    public Response<DepartmentScore> load(@RequestBody DepartmentScore departmentScore) {
        Response<DepartmentScore> response = null;
        DepartmentScore result = departmentScoreService.load(departmentScore);
        response = new Response<>(ExpCodeState.query_data_success, result);
        return response;
    }

    @ApiOperation("全部列表查询接口")
    @PostMapping("/getList")
    @ResponseBody
    public Response<List<DepartmentScore>> getList(DepartmentScore departmentScore) {
        List<DepartmentScore> result = departmentScoreService.getList(departmentScore);
        Response<List<DepartmentScore>> res = new Response<>(ExpCodeState.query_data_success,
                result);
        return res;
    }

    @ApiOperation("分页查询接口")
    @PostMapping("/getListByPage/{pageNum}/{pageSize}")
    @ResponseBody
    @MyLog(value = "查询单位打分信息")
    public Response<PageInfo<DepartmentScore>> getList(DepartmentScore departmentScore, Pages page) {
        PageInfo<DepartmentScore> result = departmentScoreService.getListByPage(departmentScore, page);
        Response<PageInfo<DepartmentScore>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("计划汇总查配合单位")
    @PostMapping("/getListDepartmentSocre")
    @ResponseBody
    public Response<List<DepartmentScoreVo>> getListDepartmentSocre(DepartmentScore departmentScore) {
        List<DepartmentScoreVo> result = departmentScoreService.getListDepartmentSocre(departmentScore);
        Response<List<DepartmentScoreVo>> res = new Response<List<DepartmentScoreVo>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("评分汇总")
    @PostMapping("/getScoreSummary")
    @ResponseBody
    public Response<List<ScoreSummaryVo>> getScoreSummary(Integer type) {
        List<ScoreSummaryVo> result = departmentScoreService.getScoreSummary(type);
        Response<List<ScoreSummaryVo>> res = new Response<List<ScoreSummaryVo>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("重点工作评分汇总")
    @PostMapping("/getScoreSummaryTask")
    @ResponseBody
    public Response<List<ScoreSummaryVo>> getScoreSummaryTask(ScoreSummaryVo scoreSummaryVo) {
        List<ScoreSummaryVo> result = departmentScoreService.getScoreSummaryTask(scoreSummaryVo);
        Response<List<ScoreSummaryVo>> res = new Response<List<ScoreSummaryVo>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("删除部门打分接口")
    @PostMapping("/removeDepartmentScore")
    @ResponseBody
    @MyLog(value = "删除部门打分")
    public Response<Integer> removeDepartmentScore(DepartmentScore departmentScore) {
        Response<Integer> response = null;
        Integer result = departmentScoreService.removeDepartmentScore(departmentScore);
        if (result > 0) {
            response = new Response<>(ExpCodeState.remove_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.remove_data_failed);
        }
        return response;
    }

    @ApiOperation("根据部门id，年，季度查询各任务的分数")
    @PostMapping("/getScoreByDeptId")
    @ResponseBody
    public Response<List<EvaluateTaskPo>> getScoreByDeptId(@RequestBody DeptTaskNumVo deptTaskNumVo) {
        if (deptTaskNumVo.getYear() == null) {
            // 默认当年
            deptTaskNumVo.setYear(DateUtil.getYear(new Date()));
        }
        List<EvaluateTaskPo> result = departmentScoreService.getScoreByDeptId(deptTaskNumVo);
        Response<List<EvaluateTaskPo>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("根据部门id查询各季度任务的分数")
    @PostMapping("/getQuarterScoreByDeptId")
    @ResponseBody
    public Response<List<DeptQuarterScoreVo>> getQuarterScoreByDeptId(@RequestBody DeptTaskNumVo deptTaskNumVo) {
        if (deptTaskNumVo.getYear() == null) {
            // 默认当年
            deptTaskNumVo.setYear(DateUtil.getYear(new Date()));
        }
        List<DeptQuarterScoreVo> result = departmentScoreService.getQuarterScoreByDeptId(deptTaskNumVo);
        Response<List<DeptQuarterScoreVo>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("根据部门类型和重点工作id查询各部门得分")
    @PostMapping("/getScoreByDeptTypeAndTaskId")
    @ResponseBody
    public Response<List<DeptTypeScoreDto>> getScoreByDeptTypeAndTaskId(@RequestBody DeptTypeTaskScoreVo deptTypeTaskScoreVo) {
        if (deptTypeTaskScoreVo.getYear() == null) {
            // 默认当年
            deptTypeTaskScoreVo.setYear(DateUtil.getYear(new Date()));
        }
        List<DeptTypeScoreDto> result = departmentScoreService.getScoreByDeptTypeAndTaskId(deptTypeTaskScoreVo);
        Response<List<DeptTypeScoreDto>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }
}
